package com.zl.zlbaseproject.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zl.zlbaseproject.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by 郑磊 on 2018/6/17.
 */

public class ZlLayoutManager extends FrameLayout {
    int mEmptyImage;
    CharSequence mEmptyText;
    int mErrorImage;
    CharSequence mErrorText;
    CharSequence mRetryText;
    OnClickListener mRetryButtonClickListener;
    OnClickListener mRetryListener;
    ZlLayoutManager.OnInflateListener mOnEmptyInflateListener;
    ZlLayoutManager.OnInflateListener mOnErrorInflateListener;
    int mTextColor;
    int mTextSize;
    int mButtonTextColor;
    int mButtonTextSize;
    int mButtonBackground;
    int mEmptyResId;
    int mLoadingResId;
    int mErrorResId;
    int mContentId;
    Map<Integer, View> mLayouts;
    LayoutInflater mInflater;

    public ZlLayoutManager(@NonNull Context context) {
        this(context, null,0 );
    }

    public ZlLayoutManager(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ZlLayoutManager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mEmptyResId = -1;
        this.mLoadingResId = -1;
        this.mErrorResId = -1;
        this.mContentId = -1;
        this.mLayouts = new HashMap();
        this.mInflater = LayoutInflater.from(context);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZlLayoutManager);
        this.mEmptyImage = a.getResourceId(R.styleable.ZlLayoutManager_zlEmptyImage, R.mipmap.icon_me);
        this.mEmptyText = a.getString(R.styleable.ZlLayoutManager_zlEmptyText);
        this.mErrorImage = a.getResourceId(R.styleable.ZlLayoutManager_zlErrorImage, R.mipmap.icon_more);
//        this.mErrorText = a.getString(R.styleable.ZlLayoutManager_zlErrorText);
//        this.mRetryText = a.getString(R.styleable.ZlLayoutManager_zlRetryText);
//        this.mTextColor = a.getColor(R.styleable.LoadingLayout_llTextColor, -6710887);
//        this.mTextSize = a.getDimensionPixelSize(R.styleable.LoadingLayout_llTextSize, this.dp2px(16.0F));
//        this.mButtonTextColor = a.getColor(R.styleable.LoadingLayout_llButtonTextColor, -6710887);
//        this.mButtonTextSize = a.getDimensionPixelSize(R.styleable.LoadingLayout_llButtonTextSize, this.dp2px(16.0F));
//        this.mButtonBackground = a.getDrawable(R.styleable.LoadingLayout_llButtonBackground);
        this.mEmptyResId = a.getResourceId(R.styleable.ZlLayoutManager_zlEmptyResId, R.layout.default_empty);
        this.mLoadingResId = a.getResourceId(R.styleable.ZlLayoutManager_zlLoadingResId, R.layout.default_loading);
        this.mErrorResId = a.getResourceId(R.styleable.ZlLayoutManager_zlErrorResId,R.layout.default_error);
        a.recycle();
    }



    protected void onFinishInflate() {
        super.onFinishInflate();
        if(this.getChildCount() != 0) {
            if(this.getChildCount() > 1) {
                this.removeViews(1, this.getChildCount() - 1);
            }

            View view = this.getChildAt(0);
            this.setContentView(view);
            this.showLoading();
        }
    }

    public void setContentView(View view) {
        this.mContentId = view.getId();
        this.mLayouts.put(Integer.valueOf(this.mContentId), view);
    }

    public ZlLayoutManager setLoading(@LayoutRes int id) {
        if(this.mLoadingResId != id) {
            this.remove(this.mLoadingResId);
            this.mLoadingResId = id;
        }
        return this;
    }


    public ZlLayoutManager setEmpty(@LayoutRes int id) {
        if(this.mEmptyResId != id) {
            this.remove(this.mEmptyResId);
            this.mEmptyResId = id;
        }
        return this;
    }

    public ZlLayoutManager setOnEmptyInflateListener(ZlLayoutManager.OnInflateListener listener) {
        this.mOnEmptyInflateListener = listener;
        if(this.mOnEmptyInflateListener != null && this.mLayouts.containsKey(Integer.valueOf(this.mEmptyResId))) {
            listener.onInflate((View)this.mLayouts.get(Integer.valueOf(this.mEmptyResId)));
        }

        return this;
    }

    public ZlLayoutManager setOnErrorInflateListener(ZlLayoutManager.OnInflateListener listener) {
        this.mOnErrorInflateListener = listener;
        if(this.mOnErrorInflateListener != null && this.mLayouts.containsKey(Integer.valueOf(this.mErrorResId))) {
            listener.onInflate((View)this.mLayouts.get(Integer.valueOf(this.mErrorResId)));
        }

        return this;
    }

    public ZlLayoutManager setEmptyImage(@DrawableRes int resId) {
        this.mEmptyImage = resId;
        this.image(this.mEmptyResId, R.id.empty_image, this.mEmptyImage);
        return this;
    }

    public ZlLayoutManager setEmptyText(String value) {
        this.mEmptyText = value;
        this.text(this.mEmptyResId, R.id.empty_text, this.mEmptyText);
        return this;
    }

    public ZlLayoutManager setErrorImage(@DrawableRes int resId) {
        this.mErrorImage = resId;
        this.image(this.mErrorResId, R.id.error_image, this.mErrorImage);
        return this;
    }

    public ZlLayoutManager setErrorText(String value) {
        this.mErrorText = value;
        this.text(this.mErrorResId, R.id.error_text, this.mErrorText);
        return this;
    }

    public ZlLayoutManager setRetryText(String text) {
        this.mRetryText = text;
        this.text(this.mErrorResId, R.id.retry_button, this.mRetryText);
        return this;
    }

    public ZlLayoutManager setRetryListener(OnClickListener listener) {
        this.mRetryListener = listener;
        return this;
    }

    public void showLoading() {
        this.show(this.mLoadingResId);
    }

    public void showEmpty() {
        this.show(this.mEmptyResId);
    }

    public void showError() {
        this.show(this.mErrorResId);
    }

    public void showContent() {
        this.show(this.mContentId);
    }

    private void show(int layoutId) {
        Iterator iterator = this.mLayouts.values().iterator();

        while(iterator.hasNext()) {
            View view = (View)iterator.next();
            view.setVisibility(View.GONE);
        }

        this.layout(layoutId).setVisibility(View.VISIBLE);
    }

    private void remove(int layoutId) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            View vg = (View)this.mLayouts.remove(Integer.valueOf(layoutId));
            this.removeView(vg);
        }

    }

    private View layout(int layoutId) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            return (View)this.mLayouts.get(Integer.valueOf(layoutId));
        } else {
            View layout = this.mInflater.inflate(layoutId, this, false);
            layout.setVisibility(View.GONE);
            this.addView(layout);
            this.mLayouts.put(Integer.valueOf(layoutId), layout);
            ImageView img;
            TextView txt;
            if(layoutId == this.mEmptyResId) {
                img = (ImageView)layout.findViewById(R.id.empty_image);
                if(img != null) {
                    img.setImageResource(this.mEmptyImage);
                }

                txt = (TextView)layout.findViewById(R.id.empty_text);
                if(txt != null) {
                    txt.setText(this.mEmptyText);
                    txt.setTextColor(this.mTextColor);
                    txt.setTextSize(0, (float)this.mTextSize);
                }

                if(this.mOnEmptyInflateListener != null) {
                    this.mOnEmptyInflateListener.onInflate(layout);
                }
            } else if(layoutId == this.mErrorResId) {
                img = (ImageView)layout.findViewById(R.id.error_image);
                if(img != null) {
                    img.setImageResource(this.mErrorImage);
                }

                txt = (TextView)layout.findViewById(R.id.error_text);
                if(txt != null) {
                    txt.setText(this.mErrorText);
                    txt.setTextColor(this.mTextColor);
                    txt.setTextSize(0, (float)this.mTextSize);
                }

                TextView btn = (TextView)layout.findViewById(R.id.retry_button);
                if(btn != null) {
                    btn.setText(this.mRetryText);
                    btn.setTextColor(this.mButtonTextColor);
                    btn.setTextSize(0, (float)this.mButtonTextSize);
                    btn.setBackgroundResource(this.mButtonBackground);
                    btn.setOnClickListener(this.mRetryButtonClickListener);
                }

                if(this.mOnErrorInflateListener != null) {
                    this.mOnErrorInflateListener.onInflate(layout);
                }
            }

            return layout;
        }
    }

    private void text(int layoutId, int ctrlId, CharSequence value) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            TextView view = (TextView)((View)this.mLayouts.get(Integer.valueOf(layoutId))).findViewById(ctrlId);
            if(view != null) {
                view.setText(value);
            }
        }

    }

    private void image(int layoutId, int ctrlId, int resId) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            ImageView view = (ImageView)((View)this.mLayouts.get(Integer.valueOf(layoutId))).findViewById(ctrlId);
            if(view != null) {
                view.setImageResource(resId);
            }
        }

    }

    public interface OnInflateListener {
        void onInflate(View var1);
    }
}
